#include "VSDepthStencilState.h"
#include "VSShader.h"
#include "VSMaterial.h"
#include "VSGraphicInclude.h"
#include "VSResourceManager.h"
#include "VSStream.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSDepthStencilState,VSBind)
VSPointer<VSDepthStencilState> VSDepthStencilState::Default;
BEGIN_ADD_PROPERTY(VSDepthStencilState,VSBind)
REGISTER_PROPERTY(m_DepthStencilDesc,DepthStencilDesc,VSProperty::F_SAVE_LOAD_CLONE)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSDepthStencilState)
ADD_INITIAL_FUNCTION_WITH_PRIORITY(InitialDefaultState)
ADD_TERMINAL_FUNCTION(TerminalDefaultState)
IMPLEMENT_INITIAL_END
bool VSDepthStencilState::InitialDefaultState()
{
	VSDepthStencilDesc DepthStencilDesc;
	Default = VSResourceManager::CreateDepthStencilState(DepthStencilDesc);
	if(!Default)
		return 0;
	return 1;
}
bool VSDepthStencilState::TerminalDefaultState()
{
	Default = NULL;
	return 1;
}
bool VSDepthStencilState::OnLoadResource(VSResourceIdentifier *&pID)
{
	if(!m_pUser)
		return 0;
	if(!m_pUser->OnLoadDepthStencilState(this,pID))
		return 0;
	return 1;
}
bool VSDepthStencilState::OnReleaseResource(VSResourceIdentifier *pID)
{
	if(!m_pUser)
		return 0;
	if(!m_pUser->OnReleaseDepthStencilState(pID))
		return 0;
	return 1;
}
VSDepthStencilState::VSDepthStencilState()
{
	m_bIsStatic = 1;
}
VSDepthStencilState::~VSDepthStencilState()
{
	ReleaseResource();
}
unsigned int VSDepthStencilState::DiskUse()const
{
	unsigned int Use = VSBind::DiskUse();

	Use += m_DepthStencilDesc.DiskUse();

	return Use;
}
bool VSDepthStencilState::Save(VSStream & rStream,unsigned int &iSaveUse)const
{
	if(!VSObject::Save(rStream,iSaveUse))
		return false;


	if (!m_DepthStencilDesc.Save(rStream,iSaveUse))
	{
		return 0;
	}
	return 1;
}
bool VSDepthStencilState::Load(VSStream & rStream,unsigned int &iSaveUse)
{

	if(!VSObject::Load(rStream,iSaveUse))
		return false;


	if(!m_DepthStencilDesc.Load(rStream,iSaveUse))
		return 0;

	return 1;
}
bool VSDepthStencilState::Clone(const VSObject *pObject,unsigned int uiType)
{
	const VSDepthStencilState* Temp = DynamicCast<VSDepthStencilState>(pObject); 
	if(!Temp)
		return 0;

	if(VSBind::Clone(pObject,uiType))
	{

		m_DepthStencilDesc = Temp->m_DepthStencilDesc;


		return 1;
	}
	else
	{
		return 0;

	}
}
bool VSDepthStencilState::Link(VSStream & rStream)
{
	if(!VSObject::Link(rStream))
		return 0;

	return 1;
}
bool VSDepthStencilState::Register(VSStream & rStream)const
{
	if (rStream.IsObjectRegister(this))
	{
		return 1;
	}
	if(!VSBind::Register(rStream))
		return 0;


	return 1;
}
IMPLEMENT_RTTI(VSDepthStencilDesc,VSObject)
BEGIN_ADD_PROPERTY(VSDepthStencilDesc,VSObject)
REGISTER_PROPERTY(m_bDepthEnable,DepthEnable,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_bDepthWritable,DepthWritable,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiDepthCompareMethod,DepthCompareMethod,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_bStencilEnable,StencilEnable,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiStencilCompareMethod,StencilCompareMethod,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiReference,Reference,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiMask,Mask,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiWriteMask,WriteMask,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiSPassZPassOP,SPassZPassOP,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiSPassZFailOP,SPassZFailOP,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiSFailZPassOP,SFailZPassOP,VSProperty::F_SAVE_LOAD_CLONE)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSDepthStencilDesc)
IMPLEMENT_INITIAL_END
bool VSDepthStencilDesc::Save(VSStream & rStream,unsigned int &iSaveUse)const
{

	if(!rStream.Write(&m_bDepthEnable,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_bDepthWritable,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiDepthCompareMethod,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_bStencilEnable,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);


	if(!rStream.Write(&m_uiStencilCompareMethod,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiReference,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiMask,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);



	if(!rStream.Write(&m_uiWriteMask,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiSPassZPassOP,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiSPassZFailOP,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_uiSFailZPassOP,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	return true;
}
bool VSDepthStencilDesc::Load(VSStream & rStream,unsigned int &iSaveUse)
{
	if(!rStream.Read(&m_bDepthEnable,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_bDepthWritable,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiDepthCompareMethod,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_bStencilEnable,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);


	if(!rStream.Read(&m_uiStencilCompareMethod,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiReference,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiMask,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);



	if(!rStream.Read(&m_uiWriteMask,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiSPassZPassOP,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiSPassZFailOP,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_uiSFailZPassOP,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);
	return true;
}